package com.qintian.rainstorm.test;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class YmserviceImpl implements YmService {

	@Override
	public String ymInfons() {
		String json = HttpClientUtil.doGet(
				"http://10.135.30.116:8080/ZJDataCenter/api/StationForecast/get7DaysForecastByStaIdAndEles?userid=wlzx&pwd=123&productcategory=ocfstationforecast&staId=58657&elements=ww12,winds,windd,cloud,t,tmax12,tmin12,rh&dataformat=json");
		WeatherInfoBo weatherInfo = WeatherInfoBo.formatToList(json, YmInfoBo.class);
		List<YmInfoBo> ymlist = (List<YmInfoBo>) weatherInfo.getDS();
		//ymlist.stream().filter(YmInfoBo -> YmInfoBo.getHours()== 12 ||YmInfoBo.getHours()== 24 ||YmInfoBo.getHours()== 36 || YmInfoBo.getHours()== 48 || YmInfoBo.getHours()== 60 || YmInfoBo.getHours()== 72 || YmInfoBo.getHours()== 84 ).collect()
         String[] i= {"12","36","60"};
         List<YmInfoBo> bos=new ArrayList<YmInfoBo>();
         for(YmInfoBo bo:ymlist) {
        	 if(java.util.Arrays.asList(i).contains(bo.getHours()+"")) {
        		 bos.add(bo);
        	 }
         }
         float fs=1;
         for(YmInfoBo bo:bos) {
        	 fs=fs*getscors(bo);
         }
         
		return getRang(fs);
	}
   
	
	
	private float getscors(YmInfoBo bo) {
		//天气
		float weather=0;
		//温度
		float temp=0;
		//风
		float win = 0;
		if(bo.getWw12()<=3) {
			weather=weather+1;
		}else if(bo.getWw12()==4) {
			weather=(float) (weather+0.6);
		}
		if(bo.getTmax12()!=-999 && bo.getTmin12()!=-999){
			float avgTem=(bo.getTmax12()+bo.getTmin12())/2;
			if(bo.getTmax12()<=32) {
				temp=temp+1;
			}else if(32<bo.getTmax12() && bo.getTmax12()<=35) {
				temp=(float) (temp+0.5);
			}else if(bo.getTmax12()>35) {
				if(avgTem<=30) {
					temp=(float) (temp+0.3);
				}
			}
		}
		if(bo.getWinds()!=-999) {
			int ws=new Integer(WindGradeUtil.windgrage(bo.getWinds())).intValue();
			if(ws<3) {
				win=win+1;
			}
		}
						
		return weather*temp*win;
	}
	
	private String getRang(float fs) {
		if(fs==0) {
			return "4";
		}else if(0<fs&&fs<=0.3) {
			return "3";
		}else if(0.3<fs&&fs<=0.5) {
			return "2";
		}else if(0.5<fs&&fs<=1) {
			return "1";
		}else {
			return "4";
		}
		
	}
}
